// pages/index.js
import React, { useEffect, useState } from 'react';
import axios from 'axios';
import { List, Spin, Alert } from 'antd';
import http from "../services/http";

export default function OrderPage() {
    const [users, setUsers] = useState([]);
    const [loading, setLoading] = useState(true);
    const [error, setError] = useState(null);

    useEffect(() => {
        http.get('/user')
            .then(response => {
                setUsers(response.data);  // 设置用户数据
                setLoading(false);  // 更新加载状态
            })
            .catch(error => {
                console.error('Error fetching users:', error);
                setError('Failed to fetch users');  // 设置错误信息
                setLoading(false);
            });
    }, []);

    if (loading) return <Spin tip="加载中..."/>;
    if (error) return <Alert message="Error" description={error} type="error" showIcon />;

    return (
        <div>
            <h1>订单列表</h1>
            <List
                itemLayout="horizontal"
                dataSource={users}
                renderItem={user => (
                    <List.Item>
                        <List.Item.Meta
                            title={<a href={`mailto:${user.email}`}>{user.name}</a>}
                            description={user.email}
                        />
                    </List.Item>
                )}
            />
        </div>
    );
}
